package com.wanmait.shoppingsys.controller.manage;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.shoppingsys.dao.GoodsDAO;
import com.wanmait.shoppingsys.dao.ManageDAO;
import com.wanmait.shoppingsys.util.ManagePager;
import com.wanmait.shoppingsys.vo.Goods;
import com.wanmait.shoppingsys.vo.Trade;

@WebServlet("/GoodsManageController")
public class GoodsManageController extends HttpServlet
{
	private GoodsDAO goodsDAO=GoodsDAO.getDAO();
	 private ManageDAO manageDAO=new ManageDAO();
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	 request.setCharacterEncoding("UTF-8");
	 String action=request.getParameter("action");
	 if(action==null)
	 {
		 return;
	 }
	 switch(action) {
	 case "goodsdelete":
		 this.goodsdelete(request, response);
			break;
	 case "trade":
		 this.trade(request,response);
		 break;
	 case "orderlist":      //全部订单列表
		 this.orderlist(request, response);
		 break;
	    }
	 
	}
	 protected void trade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		 String idStr=request.getParameter("id");
		 Integer id=Integer.parseInt(idStr);
		 ArrayList<Trade> trades = goodsDAO.FindById(id);
		 request.setAttribute("trades", trades);
		 request.getRequestDispatcher("/manage/trade.jsp").forward(request, response);
	 }
	protected void goodsdelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String goodsId = request.getParameter("goodsId");
		Integer id = Integer.parseInt(goodsId);
		manageDAO.deleteGoods(id);
		this.orderlist(request, response);
	}
	
	protected void orderlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 ManagePager managepager = ManagePager.init(request);
			manageDAO.findAll4(managepager);
			request.getRequestDispatcher("/manage/orderlist.jsp").forward(request, response);
	 }
}
